//-----------------------------------------------------------------------
// <copyright file="a_StateDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_StateDAOBase
    {
        #region Common methods
        public virtual a_State Createa_StateFromReader(IDataReader reader)
        {
            a_State item = new a_State();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("StateID"))) item.StateID = (int)reader["StateID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StateCode"))) item.StateCode = (string)reader["StateCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("StateName"))) item.StateName = (string)reader["StateName"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryISO2"))) item.CountryISO2 = (string)reader["CountryISO2"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_State methods
            
        public virtual void Createa_State(a_State a_State)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_States_ins");
                
                database.AddInParameter(dbCommand, "@StateCode", DbType.AnsiString, a_State.StateCode);
                database.AddInParameter(dbCommand, "@StateName", DbType.AnsiString, a_State.StateName);
                database.AddInParameter(dbCommand, "@CountryISO2", DbType.AnsiString, a_State.CountryISO2);
                database.AddOutParameter(dbCommand, "@StateID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_State.StateID = (int)database.GetParameterValue(dbCommand, "@StateID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_State methods
        
        public virtual void Updatea_State(a_State a_State)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_States_upd");            
                
                database.AddInParameter(dbCommand, "@StateID", DbType.Int32, a_State.StateID);
                database.AddInParameter(dbCommand, "@StateCode", DbType.AnsiString, a_State.StateCode);
                database.AddInParameter(dbCommand, "@StateName", DbType.AnsiString, a_State.StateName);
                database.AddInParameter(dbCommand, "@CountryISO2", DbType.AnsiString, a_State.CountryISO2);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_State methods
        public virtual void Deletea_State(int stateID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_States_del");
                
                database.AddInParameter(dbCommand, "@StateID", DbType.Int32, stateID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_State methods
        
        public virtual a_State Geta_State(int stateID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_States_sel");
                
                database.AddInParameter(dbCommand, "@StateID", DbType.Int32, stateID);
                
                a_State a_State =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_State = Createa_StateFromReader(reader);
                        reader.Close();
                    }
                }
                return a_State;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_StateList methods
        public virtual a_StateCollection Geta_StateList(a_StateColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_States_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_StateCollection a_StateCollection = new a_StateCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_State a_State = Createa_StateFromReader(reader);
                        a_StateCollection.Add(a_State);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_StateCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_StateCollection Geta_StateList(a_StateColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_StateList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}